package de.fhb.itunes.repository.data;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class User_VO {
	private String username;
	private String password; // als MD5 hashgespeichert
	private Playlist_VO playlist;
	private boolean isAdmin;
	private int ID;
	
	public User_VO(String username, String password) {
		super();
		this.username = username;
		this.setPassword(password); //setter, da umwandlung in md5
	}
	public User_VO(){
		super();
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return this.password;
	}
	public void setPassword(String password) {
		this.password= password;
	}
	public Playlist_VO getPlaylist(){
		return playlist;
	}
	public void setPlaylist(Playlist_VO playlist){
		this.playlist = playlist;
	}
	public void addTitelToPlaylist(int titelID){
		this.playlist.getList().add(titelID);
	}
	public boolean isAdmin(){
		return isAdmin;
	}
	public void setIsAdmin(boolean status){
		this.isAdmin = status;
	}
	public int getID() {
		return ID;
	}
	public void setID(int iD) {
		ID = iD;
	}
	public boolean validatePassword(String password){
		boolean iscorrect = false;
		
		if(this.password.equals(this.getmd5(password))){
			iscorrect = true;
		}
		
		
		return iscorrect;
	}
	private String getmd5(String input){
        String res = "";
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(input.getBytes());
            byte[] md5 = algorithm.digest();
            String tmp = "";
            for (int i = 0; i < md5.length; i++) {
                tmp = (Integer.toHexString(0xFF & md5[i]));
                if (tmp.length() == 1) {
                    res += "0" + tmp;
                } else {
                    res += tmp;
                }
            }
        } catch (NoSuchAlgorithmException ex) {}
        return res;
    }

	
}
